package com.east.config.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.east.entity.Product;
import com.east.manager.CustomCache;
import com.east.utils.Constants;

import java.util.List;

public class ProductConverter implements Converter<Integer> {
    /**
     * 返回的id
     */
    private Integer id = -1;

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData,
                                     ExcelContentProperty contentProperty,
                                     GlobalConfiguration globalConfiguration) throws Exception {
        List<Product> productList = (List<Product>) CustomCache.CACHE.getIfPresent(Constants.CACHE_PRODUCT_VALUE);
        productList.forEach(product -> {
            String stringValue = cellData.getStringValue();
            if (product.getName().equals(stringValue)) {
                id = product.getId();
            }
        });
        return id;
    }
}
